#######################################################################################################################################################
#######Code for Replicating Figures 3 and 4 in "Returning Veterans’ Attitudes toward Democracy: Evidence from a Survey of Ukraine’s ATO Veterans"######
#######by Konstantin Ash and Miroslav Shapovalov. Forthcoming in the Journal of Conflict Resolution.###################################################
#######################################################################################################################################################




###MAPS FOR FIGURE 3####

setwd("/ashshapovalov_jcr_replication/maps")
district <- readOGR(dsn="UKR_adm", layer="UKR_adm1_clean",GDAL1_integer64=TRUE)
bg <- readOGR(dsn="UKR_adm", layer="UKR_adm0",GDAL1_integer64=TRUE)
plot(bg,col="lightgray")
plot(district,col="yellow",border="gray",add=T)
text(district[ which(district@data$EXCOMNUM!='0'),], labels=district@data[ which(district@data$EXCOMNUM!='0'),]$EXCOMNUM, col='black',cex=1.5)
text(district[ which(district@data$EXCOMNUM!='0'),], labels=district@data[ which(district@data$EXCOMNUM!='0'),]$NAME_1, col='black',cex=.85, pos=3)

district <- readOGR(dsn="UKR_adm", layer="UKR_adm1",GDAL1_integer64=TRUE)
bg <- readOGR(dsn="UKR_adm", layer="UKR_adm0",GDAL1_integer64=TRUE)
plot(bg,col="lightgray")
plot(district,col="yellow",border="gray",add=T)
text(district[ which(district@data$EXCOMMOVEN!='0'),], labels=district@data[ which(district@data$EXCOMMOVEN!='0'),]$EXCOMMOVEN, col='black',cex=1.5)
text(district[ which(district@data$EXCOMMOVEN!='0'),], labels=district@data[ which(district@data$EXCOMMOVEN!='0'),]$NAME_1, col='black',cex=.7, pos=3)

cities <- readOGR(dsn=".", layer="Lab-in-the-field Cities",GDAL1_integer64=TRUE)
cities@data$CITYNAME[2] <- "Vinnytsia"
plot(bg,col="lightgray",border="darkgray")
plot(district,col="yellow",border="darkgray",add=T)
plot(cities,col="lightblue",pch=21,add=T,cex=1.5)
text(cities, labels=cities@data$CITYNAME, col='darkblue',cex=1.5, pos=1)

####FIGURE 4 Bar Plots####
library(ggplot2)
library(scales) 
library(reshape2)
library(foreign)

setwd("/ashshapovalov_jcr_replication")
democracy1<-read.csv('democracy1.csv')
democracy2<-read.csv('democracy2.csv')
democracy3<-read.csv('democracy3.csv')
democracy4<-read.csv('democracy4.csv')


ggplot(democracy1,
       aes(x = factor(response, level=c('Strongly Disagree', 'Disagree', 'Neither Agree nor Disagree', 'Agree', 'Strongly Agree')), y = percentage,  fill = sample)) +
  geom_bar(stat = "identity", position = position_dodge(), alpha = 0.75) + 
  scale_x_discrete(labels = label_wrap(10)) + ggtitle("Democracy is the best system")+
  scale_y_continuous(expand = c(0,0),
                     limits = c(0,45)) + 
  scale_fill_manual(values=c('blue', 'yellow3'))+
  labs(x = NULL, y = NULL)+
  theme(plot.title = element_text(size=20, face="bold", hjust = 0.5),legend.position="top",legend.justification="left",legend.title=element_blank())+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

ggplot(democracy2,
       aes(x = factor(response, level=c('Strongly Disagree', 'Disagree', 'Neither Agree nor Disagree', 'Agree', 'Strongly Agree')), y = percentage,  fill = sample)) +
  geom_bar(stat = "identity", position = position_dodge(), alpha = 0.75) + 
  scale_x_discrete(labels = label_wrap(10)) + ggtitle("Ukrainians rarely elect suitable people")+
  scale_y_continuous(expand = c(0,0),
                     limits = c(0,55)) + 
  scale_fill_manual(values=c('blue', 'yellow3'))+
  labs(x = NULL, y = NULL)+
  theme(plot.title = element_text(size=20, face="bold", hjust = 0.5),legend.position="top",legend.justification="left",legend.title=element_blank())+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

ggplot(democracy3,
       aes(x = factor(response, level=c('Strongly Disagree', 'Disagree', 'Neither Agree nor Disagree', 'Agree', 'Strongly Agree')), y = percentage)) +
  geom_bar(stat = "identity", position = position_dodge(), alpha = 0.75, fill="blue") + 
  scale_x_discrete(labels = label_wrap(10)) + ggtitle("ATO veterans can best solve conflict")+
  scale_y_continuous(expand = c(0,0),
                     limits = c(0,30)) + 
  labs(x = NULL, y = NULL)+
  theme(plot.title = element_text(size=20, face="bold", hjust = 0.5),legend.position = "none")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

ggplot(democracy4,
       aes(x = factor(response, level=c('Strongly Disagree', 'Disagree', 'Neither Agree nor Disagree', 'Agree', 'Strongly Agree')), y = percentage)) +
  geom_bar(stat = "identity", position = position_dodge(), alpha = 0.75, fill="blue") + 
  scale_x_discrete(labels = label_wrap(10)) + ggtitle("ATO veterans can better solve problems")+
  scale_y_continuous(expand = c(0,0),
                     limits = c(0,30)) + 
  labs(x = NULL, y = NULL)+
  theme(plot.title = element_text(size=20, face="bold", hjust = 0.5),legend.position = "none")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))
